﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：ProductManage.ascx.cs
//
//文件功能描述： 商品管理
// 
//创建标识：2007.11.22 施欢
//创建描述：创建页面程序逻辑
//
//修改标识: 2007.11.28 施欢 
//修改描述: 按编码规范对代码进行了调整
//
//修改标识: 2007.12.04 施欢 
//修改描述: 使用分页方法获取商品列表
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 商品管理
/// </summary>
public partial class UserControls_ProductManage : System.Web.UI.UserControl
{
    private static DataSet ds = new DataSet();  //保存商品列表
    private static int PAGESIZE = 10;           //GridView控件的分页大小
    private static int PAGEINDEX = 1;           //第一页
    protected void Page_Load(object sender, EventArgs e)
    {
        //初次加载时初始化页面
        if (!Page.IsPostBack) 
        {
            
            localhost.ProductService service1 = new localhost.ProductService();
            service1.GetLowCategoryListCompleted += new localhost.GetLowCategoryListCompletedEventHandler(service_GetLowerCategoryListCompleted);

            //异步获取商品小类别列表
            service1.GetLowCategoryListAsync();

            //设置默认搜索条件
            string strProduct = string.Empty;
            string strLowerCategoryID = string.Empty;

            //获取默认条件下的商品记录总数和商品页数
            GetRowsCount(strProduct,strLowerCategoryID);

            //显示所有的商品
            GetProductList(PAGESIZE,PAGEINDEX,strProduct,strLowerCategoryID);
        }

    }

    /// <summary>
    /// 获取指定条件下的商品总数,保存在隐藏控件hdfProductCount中,并计算出商品页数
    /// 保存在隐藏控件hdfPageCount中 
    /// </summary>
    /// <param name="strProductName">商品名称</param>
    /// <param name="strLowerCategoryID">商品小类别编号</param>
    private void GetRowsCount(string strProductName,string strLowerCategoryID)
    {
        localhost.ProductService service = new localhost.ProductService();

        //获取指定条件下的商品总数
        int rowsCount = service.GetProductRows(strProductName,strLowerCategoryID);
        hdfProductCount.Value = rowsCount.ToString();

        //计算出商品页数
        int pagesCount = rowsCount / PAGESIZE;
        if (rowsCount % PAGESIZE != 0)
        {
            pagesCount = pagesCount + 1;
        }
        hdfPageCount.Value = pagesCount.ToString();
    }
    
    /// <summary>
    /// 实行"管理'或"删除"操作
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    protected void gdvProductManage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strProductID = e.CommandArgument.ToString();

        //选中"管理"时转到"商品修改"页面
        if (e.CommandName == "Manage")
        {
            Response.Redirect("~/StoreKeeper/ProductAdd.aspx?ProductID=" + strProductID);
        }

        //选中"删除"时实行删除操作
        else if (e.CommandName == "LogicalDelete")
        {
            localhost.ProductService productDelete = new localhost.ProductService();

            //获取删除操作结果
            bool result = productDelete.DeleteProduct(strProductID);

            //删除成功更新商品列表
            if (result == true)
            {
                string strProductName = txtProductName.Text.Trim();
                string strLowerCategoryID = dropCatagories.SelectedValue.ToString();
                int currentPage = Convert.ToInt32(hdfCurrentPage.Value);
                GetProductList(PAGESIZE, currentPage, strProductName, strLowerCategoryID);
            }
            else
            {
                Page.RegisterStartupScript("warnings", "<script>alert('删除失败！');</script>");
            }
        }

    }

    /// <summary>
    /// 实行"搜索"操作
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string strProductName = txtProductName.Text.Trim();      //商品名称
        string strLowerCategoryID = dropCatagories.SelectedValue;//商品小类别编号

        //计算指定条件下的商品总数和商品页数
        GetRowsCount(strProductName, strLowerCategoryID);

        //显示搜索结果
        GetProductList(PAGESIZE, PAGEINDEX, strProductName, strLowerCategoryID);
    }

    /// <summary>
    /// 按条件搜索商品
    /// </summary>
    /// <param name="pageSize">页大小</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="strProductName">商品名称</param>
    /// <param name="strLowerCategoryID">商品小类别编号</param>
    private void GetProductList(int pageSize,int pageIndex,string strProductName,string strLowerCategoryID)
    {
        hdfCurrentPage.Value = pageIndex.ToString();  //设置当前页
        localhost.ProductService service = new localhost.ProductService();
        service.GetProductCompleted += new localhost.GetProductCompletedEventHandler(service_GetProductCompleted);
        service.GetProductAsync(pageSize,pageIndex,strProductName,strLowerCategoryID);
    }

    /// <summary>
    /// 搜索商品的回调函数
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    private void service_GetProductCompleted(object sender,localhost.GetProductCompletedEventArgs e)
    {
        ds = e.Result;
        GridviewBind(ds);
    }

    /// <summary>
    /// 获取小类别列表的回调函数
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    private void service_GetLowerCategoryListCompleted(object sender,localhost.GetLowCategoryListCompletedEventArgs e)
    {
        DataSet dsLowerCategories = new DataSet();
        dsLowerCategories = e.Result;
        dropCatagories.DataSource = dsLowerCategories.Tables[0];
        dropCatagories.DataTextField = "Name";
        dropCatagories.DataValueField = "LowerCategoryID";
        dropCatagories.DataBind();
        dropCatagories.Items.Add("全部"); //添加"全部"选项
        dropCatagories.Items[dropCatagories.Items.Count - 1].Value = string.Empty;
        dropCatagories.SelectedIndex = dropCatagories.Items.Count-1; //显示 "全部"
    }
    
    /// <summary>
    /// 绑定商品列表
    /// </summary>
    /// <param name="ds">要绑定的数据</param>
    private void GridviewBind(DataSet ds)
    {
        int pageCount = Convert.ToInt32(hdfPageCount.Value);
        try
        {
            this.gdvProductManage.DataSource = ds.Tables[0].DefaultView;
            this.gdvProductManage.DataBind();

            //初始化控件
            btnFirst.Visible = true;
            btnPrev.Visible = true;
            btnNext.Visible = true;
            btnLast.Visible = true;

            LblCurrentIndex.Visible = true;
            LblPageCount.Visible = true;
            LblRecordCount.Visible = true;
            LblNoRecord.Visible = false;
            
            LblCurrentIndex.Text = "第 " + hdfCurrentPage.Value.ToString() + " 页";
            LblPageCount.Text = "共 " +  hdfPageCount.Value.ToString() + " 页";
            LblRecordCount.Text = "总共 " + hdfProductCount.Value.ToString() + " 条";

            //商品总数为0时显示"记录为零"
            if (ds.Tables[0].Rows.Count == 0)
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = false;
                LblPageCount.Visible = false;
                LblRecordCount.Visible = false;

                LblNoRecord.Visible = true;
            }

            //只有1页商品时设置相应控件不可见
            else if (pageCount == 1)
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
                LblNoRecord.Visible = false;//
            }
        }
        catch (Exception ex)
        {
            Response.Write("发生错误,错误原因：" + ex.Message);
            Response.End();
        }
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();

        //获取搜索条件
        string strProductName = txtProductName.Text.Trim();
        string strCategoryID = dropCatagories.SelectedValue.ToString();
        
        int currentPage = Convert.ToInt32( hdfCurrentPage.Value);
        int pageCount = Convert.ToInt32(hdfPageCount.Value);
        switch (arg)
        {
            case "prev":
                if (currentPage > 1)
                {
                    currentPage -= 1;
                }
                break;
            case "next":
                if (currentPage < pageCount)
                {
                    currentPage += 1;
                }
                break;
            case "last":
                currentPage = pageCount;
                break;
            default:
                currentPage = 1;
                break;
        }
        hdfCurrentPage.Value = currentPage.ToString();

        //更新商品列表
        GetProductList(PAGESIZE, currentPage, strProductName, strCategoryID);
    }
}
